package com.gitee.wsl.struct.window


/**
 * Wrapper entity class for a period of time window.
 *
 * @param <T> data type
 * @author jialiang.linjl
 * @author Eric Zhao
</T> */
class WindowWrap<T>(
    /**
     * Time length of a single window bucket in milliseconds.
     * 窗口的长度
     */
    val windowLengthInMs: Long,
    /**
     * Start timestamp of the window in milliseconds.
     * 窗口的开始时间
     */
    var windowStart: Long,
    /**
     * Statistic data.
     * 需要的统计的数据
     */
     var value: T,
) {

    /**
     * Reset start timestamp of current bucket to provided time.
     *
     * @param startTime valid start timestamp
     * @return bucket after reset
     */
    fun resetTo(startTime: Long): WindowWrap<T> {
        this.windowStart = startTime
        return this
    }

    /**
     * Check whether given timestamp is in current bucket.
     *
     * @param timeMillis valid timestamp in ms
     * @return true if the given time is in current bucket, otherwise false
     * @since 1.5.0
     */
    fun isTimeInWindow(timeMillis: Long): Boolean {
        return windowStart <= timeMillis && timeMillis < windowStart + windowLengthInMs
    }

    override fun toString(): String {
        return "WindowWrap{" +
                "windowLengthInMs=" + windowLengthInMs +
                ", windowStart=" + windowStart +
                ", value=" + value +
                '}'
    }
}
